﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tetris
{
    abstract class Figures
    {
        
        public List<Node> nodes = new List<Node>();

        protected ConsoleColor figureColor { get; set; }
        protected FigurePosition currentPosition = FigurePosition.North;


        public void MoveFigureDown()
        {
            nodes[0].MoveDown();
            nodes[1].MoveDown();
            nodes[2].MoveDown();
            nodes[3].MoveDown();
        }

        public void MoveFigureLeft()
        {
            nodes[0].MoveLeft();
            nodes[1].MoveLeft();
            nodes[2].MoveLeft();
            nodes[3].MoveLeft();
        }

        public void MoveFigureRight()
        {
            nodes[0].MoveRight();
            nodes[1].MoveRight();
            nodes[2].MoveRight();
            nodes[3].MoveRight();
        }

        virtual public void FigureRotate()
        {
        }

    }

 
    class O_Figure : Figures
    {
        public O_Figure(GameField field)
        {
            figureColor = ConsoleColor.Magenta;
            nodes.Add(new Node(field.StartX + (field.Width / 2), field.StartY + 1, figureColor));
            nodes.Add(new Node(nodes[0].XNode, nodes[0].YNode - 1, figureColor));
            nodes.Add(new Node(nodes[0].XNode + 1, nodes[0].YNode - 1, figureColor));
            nodes.Add(new Node(nodes[0].XNode + 1, nodes[0].YNode, figureColor));
            
        }
    }

    class T_Figure : Figures
    {
        public T_Figure(GameField field)
        {
            figureColor = ConsoleColor.Red;
            nodes.Add(new Node(field.StartX + (field.Width / 2), field.StartY + 1, figureColor));
            nodes.Add(new Node(nodes[0].XNode - 1, nodes[0].YNode + 1, figureColor));
            nodes.Add(new Node(nodes[0].XNode, nodes[0].YNode + 1, figureColor));
            nodes.Add(new Node(nodes[0].XNode + 1, nodes[0].YNode + 1, figureColor));
        }

        public override void FigureRotate()
        {
            if (currentPosition == FigurePosition.North)
            {
                nodes[0].XNode += 1; nodes[0].YNode += 1;
                nodes[1].XNode += 1; nodes[1].YNode -= 1;
                nodes[3].XNode -= 1; nodes[3].YNode += 1;
                currentPosition = FigurePosition.East;
            }
            else if (currentPosition == FigurePosition.East)
            {
                nodes[0].XNode -= 1; nodes[0].YNode += 1;
                nodes[1].XNode += 1; nodes[1].YNode += 1;
                nodes[3].XNode -= 1; nodes[3].YNode -= 1;
                currentPosition = FigurePosition.South;
            }
            else  if (currentPosition == FigurePosition.South)
            {
                nodes[0].XNode -= 1; nodes[0].YNode -= 1;
                nodes[1].XNode -= 1; nodes[1].YNode += 1;
                nodes[3].XNode += 1; nodes[3].YNode -= 1;
                currentPosition = FigurePosition.West;
            }
            else if (currentPosition == FigurePosition.West)
            {
                nodes[0].XNode += 1; nodes[0].YNode -= 1;
                nodes[1].XNode -= 1; nodes[1].YNode -= 1;
                nodes[3].XNode += 1; nodes[3].YNode += 1;
                currentPosition = FigurePosition.North;
            }
        }     
    }

    class J_Figure : Figures
    {
        public J_Figure(GameField field)
        {
            figureColor = ConsoleColor.Green;
            nodes.Add(new Node(field.StartX + (field.Width / 2), field.StartY + 1, figureColor));
            nodes.Add(new Node(nodes[0].XNode, nodes[0].YNode + 1, figureColor));
            nodes.Add(new Node(nodes[0].XNode + 1, nodes[0].YNode + 1, figureColor));
            nodes.Add(new Node(nodes[0].XNode + 2, nodes[0].YNode + 1, figureColor));
        }

        public override void FigureRotate()
        {
            if (currentPosition == FigurePosition.North)
            {
                nodes[0].XNode += 2;
                nodes[1].XNode += 1; nodes[1].YNode -= 1;
                nodes[3].XNode -= 1; nodes[3].YNode += 1;
                currentPosition = FigurePosition.East;
            }

            else if (currentPosition == FigurePosition.East)
            {
                nodes[0].YNode += 2;
                nodes[1].XNode += 1; nodes[1].YNode += 1;
                nodes[3].XNode -= 1; nodes[3].YNode -= 1;
                currentPosition = FigurePosition.South;
            }

            else if(currentPosition == FigurePosition.South)
            {
                nodes[0].XNode -= 2;
                nodes[1].XNode -= 1; nodes[1].YNode += 1;
                nodes[3].XNode += 1; nodes[3].YNode -= 1;
                currentPosition = FigurePosition.West;
            }

            else if(currentPosition == FigurePosition.West)
            {
                nodes[0].YNode -= 2;
                nodes[1].XNode -= 1; nodes[1].YNode -= 1;
                nodes[3].XNode += 1; nodes[3].YNode += 1;
                currentPosition = FigurePosition.North;
            }
        }
    }

    class L_Figure : Figures
    {
        public L_Figure(GameField field)
        {
            figureColor = ConsoleColor.DarkMagenta;
            nodes.Add(new Node(field.StartX + (field.Width / 2), field.StartY + 1, figureColor));
            nodes.Add(new Node(nodes[0].XNode, nodes[0].YNode + 1, figureColor));
            nodes.Add(new Node(nodes[0].XNode - 1, nodes[0].YNode + 1, figureColor));
            nodes.Add(new Node(nodes[0].XNode - 2, nodes[0].YNode + 1, figureColor));
        }

        public override void FigureRotate()
        {
            if (currentPosition == FigurePosition.North)
            {
                nodes[0].YNode += 2;
                nodes[1].XNode -= 1; nodes[1].YNode += 1;
                nodes[3].XNode += 1; nodes[3].YNode -= 1;
                currentPosition = FigurePosition.East;
            }

            else if (currentPosition == FigurePosition.East)
            {
                nodes[0].XNode -= 2;
                nodes[1].XNode -= 1; nodes[1].YNode -= 1;
                nodes[3].XNode += 1; nodes[3].YNode += 1;
                currentPosition = FigurePosition.South;
            }

            else if (currentPosition == FigurePosition.South)
            {
                nodes[0].YNode -= 2;
                nodes[1].XNode += 1; nodes[1].YNode -= 1;
                nodes[3].XNode -= 1; nodes[3].YNode += 1;
                currentPosition = FigurePosition.West;
            }

            else if (currentPosition == FigurePosition.West)
            {
                nodes[0].XNode += 2;
                nodes[1].XNode += 1; nodes[1].YNode += 1;
                nodes[3].XNode -= 1; nodes[3].YNode -= 1;
                currentPosition = FigurePosition.North;
            }
        }
    }

    class Z_Figure : Figures
    {
        public Z_Figure(GameField field)
        {
            figureColor = ConsoleColor.Blue;
            nodes.Add(new Node(field.StartX + (field.Width / 2), field.StartY + 1, figureColor));
            nodes.Add(new Node(nodes[0].XNode + 1, nodes[0].YNode, figureColor));
            nodes.Add(new Node(nodes[0].XNode + 1, nodes[0].YNode + 1, figureColor));
            nodes.Add(new Node(nodes[0].XNode + 2, nodes[0].YNode + 1, figureColor));
        }

        public override void FigureRotate()
        {
            if (currentPosition == FigurePosition.North)
            {
                nodes[0].XNode += 2;
                nodes[1].XNode += 1; nodes[1].YNode += 1;
                nodes[3].XNode -= 1; nodes[3].YNode += 1;
                currentPosition = FigurePosition.West;
            }
            else if (currentPosition == FigurePosition.West)
            {
                nodes[0].XNode -= 2;
                nodes[1].XNode -= 1; nodes[1].YNode -= 1;
                nodes[3].XNode += 1; nodes[3].YNode -= 1;
                currentPosition = FigurePosition.North;
            }

        }
    }

    class S_Figure : Figures
    {
        public S_Figure(GameField field)
        {
            figureColor = ConsoleColor.DarkRed;
            nodes.Add(new Node(field.StartX + (field.Width / 2), field.StartY + 1, figureColor));
            nodes.Add(new Node(nodes[0].XNode - 1, nodes[0].YNode, figureColor));
            nodes.Add(new Node(nodes[0].XNode - 1, nodes[0].YNode + 1, figureColor));
            nodes.Add(new Node(nodes[0].XNode - 2, nodes[0].YNode + 1, figureColor));
        }

        public override void FigureRotate()
        {
            if (currentPosition == FigurePosition.North)
            {
                nodes[0].XNode -= 2;
                nodes[1].XNode -= 1; nodes[1].YNode += 1;
                nodes[3].XNode += 1; nodes[3].YNode += 1;
                currentPosition = FigurePosition.West;
            }
            else if (currentPosition == FigurePosition.West)
            {
                nodes[0].XNode += 2;
                nodes[1].XNode += 1; nodes[1].YNode -= 1;
                nodes[3].XNode -= 1; nodes[3].YNode -= 1;
                currentPosition = FigurePosition.North;
            }

        }
    }

    class I_Figure : Figures
    {
        public I_Figure(GameField field)
        {
            figureColor = ConsoleColor.White;
            nodes.Add(new Node(field.StartX + (field.Width / 2), field.StartY + 1, figureColor));
            nodes.Add(new Node(nodes[0].XNode + 1, nodes[0].YNode, figureColor));
            nodes.Add(new Node(nodes[0].XNode + 2, nodes[0].YNode, figureColor));
            nodes.Add(new Node(nodes[0].XNode + 3, nodes[0].YNode, figureColor));
        }

        public override void FigureRotate()
        {
            if (currentPosition == FigurePosition.North)
            {
                nodes[0].XNode += 2; nodes[0].YNode -= 2;
                nodes[1].XNode += 1; nodes[1].YNode -= 1;
                nodes[3].XNode -= 1; nodes[3].YNode += 1;
                currentPosition = FigurePosition.West;
            }
            else if (currentPosition == FigurePosition.West)
            {
                nodes[0].XNode -= 2; nodes[0].YNode += 2;
                nodes[1].XNode -= 1; nodes[1].YNode += 1;
                nodes[3].XNode += 1; nodes[3].YNode -= 1;
                currentPosition = FigurePosition.North;
            }

        }

    }
}
